<template>
    <div>
        <el-form ref="formSearch" :model="formSearch" label-width="100px" size="small" :inline="true">
            <el-form-item>
                <el-button type="primary" size="small" @click="handleAddOrUpdate">添加</el-button>
            </el-form-item>
            <el-form-item>
                <el-button size="small" plain @click.stop="typeManage">分类管理</el-button>
            </el-form-item>
            <el-form-item>
                <el-button size="small" plain @click.stop="SampleCaseManage">留样条件</el-button>
            </el-form-item>
            <!--      <el-form-item>-->
            <!--        <el-button size="small" plain @click.stop='SampleCaseManage'>留样管理</el-button>-->
            <!--      </el-form-item>-->
            <el-form-item label="留样状态:">
                <el-select v-model="formSearch.status" placeholder="留样状态" style="width:100px">
                    <el-option v-for="(item, index) in statusList"
                               :key="item.value"
                               :label="item.label"
                               :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="样品分类:">
                <el-cascader v-model="formSearch.type"
                             :options="sampleTypeTreeSelect"
                             clearable
                             style="width:100%"
                >
                    <template slot-scope="{ node, data }">
                        <span>{{ data.label }}</span>
                        <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
                    </template>
                </el-cascader>
            </el-form-item>
            <el-form-item label="留样时间:">
                <el-date-picker v-model="formSearch.date"
                                type="daterange"
                                range-separator="至"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="样品编码:">
                <el-input v-model="formSearch.code" placeholder="请输入审样品编码"></el-input>
            </el-form-item>
            <el-form-item label="样品名称:">
                <el-input v-model="formSearch.name" placeholder="请输入样品名称"></el-input>
            </el-form-item>
            <el-form-item label="委托单位:">
                <el-input v-model="formSearch.entrustUnit" placeholder="请输入委托单位"></el-input>
            </el-form-item>
            <el-form-item label="委托人:">
                <el-input v-model="formSearch.client" placeholder="请输入委托人"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="searchFormSubmit()">搜索</el-button>
            </el-form-item>
            <el-form-item>
                <el-button @click="handleReset">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- 表格 -->
                   <!-- :cell-style="cellStyle" -->
        <avue-crud :option="tableOption"
                   :data="gridData"
                   :table-loading="tableLoading"
                   :page="page"
                   @current-change='currentPage'
                   @size-change="sizePage"
                   ref="crud">
            <template slot-scope="scope" slot="isRetention">{{ scope.row.isRetention == 0 ? '未留样' : '已留样' }}</template>
            <template slot-scope="scope" slot="entrustUnit">{{ scope.row.entrustUnit }} <span>/</span> {{
                scope.row.entrustUnitPhone }}
            </template>
            <template slot-scope="scope" slot="client">{{ scope.row.client }} <span>/</span> {{ scope.row.clientPhone }}
            </template>

            <template slot-scope="scope" slot="editTime">
                {{ scope.row.updateName ? scope.row.updateName : '无'}} <span>/</span> {{ scope.row.updateTime ?
                scope.row.updateTime : '无' }}
            </template>
            <!--      <template slot-scope="scope" slot="sampleType">{{ scope.row.sampleTypeId }}</template>-->
            <template slot-scope="scope" slot="menu">
                <el-button type="primary" size="small" plain @click.stop="handleAddOrUpdate(scope.row,scope.index)">修改
                </el-button>
                <el-button type="primary" size="small" plain @click.stop="handleDelete(scope.row,scope.index)">删除
                </el-button>
                <!--            <el-button type="primary" size="small"  plain @click.stop="handlePrint(scope.row,scope.index)">打印标签</el-button>-->
                <el-button type="primary" size="small" plain @click.stop="handleKeep(scope.row,scope.index)">留样
                </el-button>
            </template>
        </avue-crud>

        <!-- 添加/修改样品 -->
        <el-dialog :title="showDialogAdd ? dialogTitle : '修改样品'" :visible.sync="dialogFormVisible" width="50%">
            <el-form :model="dialogForm" :rules="rules" ref="dialogForm" label-position="right" label-width="100px">
                <el-row>
                    <el-col :span="11">
                        <el-form-item label="样品分类：" prop='type'>
                            <el-cascader v-model="dialogForm.type"
                                         :options="sampleTypeTreeSelect"
                                         :show-all-levels="false"
                                         @change="handleSelected">
                            </el-cascader>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11" :offset="2">
                        <el-form-item label="样品名称：" prop='name'>
                            <el-input v-model="dialogForm.name" placeholder=""></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="样品规格：" prop='specs'>
                            <el-input v-model="dialogForm.specs" placeholder=""></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11" :offset="2">
                        <el-form-item label="样品数量：" prop='num'>
                            <el-input v-model="dialogForm.num" placeholder=""></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="委托人：" prop='contactName'>
                            <el-input v-model="dialogForm.contactName" placeholder=""></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11" :offset="2">
                        <el-form-item label="联系电话：" prop='phone'>
                            <el-input v-model="dialogForm.phone" placeholder=""></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="委托单位：" prop='companyName'>
                            <el-input v-model="dialogForm.companyName" placeholder=""></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11" :offset="2">
                        <el-form-item label="单位电话：" prop='companyPhone'>
                            <el-input v-model="dialogForm.companyPhone" placeholder=""></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="委托时间：" prop="date">
                            <el-date-picker v-model="dialogForm.date" type="date" placeholder="选择日期"
                                            style="width:100%"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="单位地址：" prop='companyAddress'>
                            <el-input v-model="dialogForm.companyAddress" placeholder=""></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="备注：">
                            <el-input v-model="dialogForm.remarks" type="textarea" placeholder=""></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer" style="text-align:center">
                <el-button @click="dialogFormVisible = false">关 闭</el-button>
                <el-button type="primary" @click="handleAddLine('dialogForm')">确 认</el-button>

            </div>
        </el-dialog>

        <!-- 样品分类管理 -->
        <el-dialog title="样品分类" :visible.sync="dialogTypeManageVisible" width="40%">
            <div>
                <el-tree :data="sampleTypeList" :props="treeOption" :expand-on-click-node="false" default-expand-all
                         @node-click="handleNodeClick">
                <span class="custom-tree-node" slot-scope="{ node, data }">
                    <span>{{ node.label }}</span>
                    <span>
                        <el-button type="text" size="mini" @click="() => addTree(node,data)">添加</el-button>
                        <el-button type="text" size="mini" @click="() => editTree(node,data)">修改</el-button>
                        <el-button type="text" size="mini" @click="() => removeTree(node, data)">删除</el-button>
                    </span>
                </span>
                </el-tree>
            </div>
            <div slot="footer" class="dialog-footer" style="text-align:center">
                <el-button size="medium" type="primary" @click="addTreeNoP()">添加</el-button>
                <el-button size="medium" @click="dialogTypeManageVisible = false">关 闭</el-button>
            </div>
        </el-dialog>

        <!-- 添加分类 -->
        <el-dialog title="添加分类" :visible.sync="dialogFormTypeVisible" width="40%">
            <el-form ref="dialogFormType" :model="dialogFormType" :rules="rulesType" label-position="right"
                     label-width="110px">
                <el-form-item label="样品分类：">
                    <el-input v-model="dialogFormType.parentId" :disabled="true" v-show="false"></el-input>
                    <el-input v-model="dialogFormType.parentIdname" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="" prop='content' label-width="110px">
                    <el-input v-model="dialogFormType.content"
                              type="textarea"
                              :autosize='{ minRows: 6, maxRows: 10}'
                              placeholder="请输入分类"
                              @keyup.enter.native="changeInput"></el-input>
                </el-form-item>
                
                <!-- placeholder="在每个分类后面敲击 “ 回车键 ” “换行”，可批量添加分类。" -->
            </el-form>
            <div slot="footer" class="dialog-footer" style="text-align:center">
                <el-button @click="dialogFormTypeVisible = false">关 闭</el-button>
                <el-button type="primary" @click="handleAddType('dialogFormType')">确 认</el-button>
            </div>
        </el-dialog>

        <!-- 修改样品分类名字 -->
        <el-dialog title="修改分类" :visible.sync="dialogTypeTitleVisible" width="40%">
            <el-form ref="dialogTypeTitle" :model="dialogTypeTitle" :rules="rulesTypeTitle" label-position="right"
                     label-width="100px">
                <el-form-item label="样品分类：">
                    <el-input v-model="dialogTypeTitle.parentId" :disabled="true" v-show="false"></el-input>
                    <el-input v-model="dialogTypeTitle.parentIdname" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="分类选择：">
                    <el-tree :data="sampleTypeList" :props="treeOption" :expand-on-click-node="false" default-expand-all
                             @node-click="handleNodeClick2"></el-tree>
                </el-form-item>
                <el-form-item label="类型名称：" prop='name'>
                    <el-input v-model="dialogTypeTitle.name" placeholder=""></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" style="text-align:center">
                <el-button @click="dialogTypeTitleVisible = false">关 闭</el-button>
                <el-button type="primary" @click="handleEditTypeTitle('dialogTypeTitle')">确 认</el-button>
            </div>
        </el-dialog>

        <!-- 留样条件 -->
        <el-dialog title="留样条件" :visible.sync="dialogSampleCaseVisible" width="40%">
            <div>
                <div class="add-type">
                    <el-button type="primary" size="small" @click="appendSampleCase()">添加</el-button>
                </div>
                <ul>
                    <li class="custom-tree-node"
                        v-for='(item, index) in conditionList'
                        :key='index'
                    >
                        <span>{{ index + ' ' + ' ' + item.label }}</span>
                        <span>
                        <el-button type="text" size="mini" @click="editSampleCase(item, index)">修改</el-button>
                        <el-button type="text" size="mini" @click="deleteSampleCase(item, index)">删除</el-button>
                    </span>
                    </li>
                </ul>
            </div>
            <div slot="footer" class="dialog-footer" style="text-align:center">
                <el-button @click="dialogSampleCaseVisible = false">关 闭</el-button>
            </div>
        </el-dialog>

        <!-- 新增修改 留样条件 -->
        <el-dialog :title="caseTitleAdd ? '修改留样条件' : '新增留样条件'" :visible.sync="dialogCaseTitleVisible" width="40%">
            <el-form ref="dialogCaseTitle" :model="dialogCaseTitle" :rules="rulesCaseTitle" label-position="right">
                <el-form-item label="" prop='name'>
                    <el-input v-model="dialogCaseTitle.name" placeholder=""></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" style="text-align:center">
                <el-button @click="dialogCaseTitleVisible = false">关 闭</el-button>
                <el-button type="primary" @click="handleCaseTitle('dialogCaseTitle')">确 认</el-button>
            </div>
        </el-dialog>

        <!-- 添加样品留样-->
        <el-dialog title="样品留样" :visible.sync="dialogAddSampleVisible" width="30%">
            <el-form ref="dialogAddSmpForm" :model="dialogAddSmpForm" :rules="rulesAddAmp" label-position="right"
                     label-width="130px">
                <el-form-item label="留样数量：" prop='num'>
                    <el-input v-model="dialogAddSmpForm.num" type="number" placeholder=""></el-input>
                </el-form-item>
                <el-form-item label="留样人：" prop='name'>
                    <el-input v-model="dialogAddSmpForm.name" placeholder=""></el-input>
                </el-form-item>
                <el-form-item label="留样时间：" prop="date">
                    <el-date-picker v-model="dialogAddSmpForm.date" format="yyyy-MM-dd" type="date" placeholder="选择日期"
                                    style="width:100%"></el-date-picker>
                </el-form-item>
                <el-form-item label="过期时间：" prop="overdueDate">
                    <el-date-picker v-model="dialogAddSmpForm.overdueDate" format="yyyy-MM-dd" type="date"
                                    placeholder="选择日期" style="width:100%"></el-date-picker>
                </el-form-item>
                <el-form-item label="过期预警(天)：" prop='warnTime'>
                    <el-input v-model="dialogAddSmpForm.warnTime" placeholder=""></el-input>
                </el-form-item>
                <el-form-item label="储存仓库：" prop='store'>
                    <el-select v-model="dialogAddSmpForm.store" placeholder="请选择" style="width:100%">
                        <el-option v-for="(item, index) in warehouseList"
                                   :key='item.value'
                                   :label="item.label"
                                   :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="储存条件：" prop='condition'>
                    <el-select v-model="dialogAddSmpForm.condition" placeholder="请选择" style="width:100%">
                        <el-option v-for="(item,index) in conditionList"
                                   :key='item.value'
                                   :label="item.label"
                                   :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="   备注：">
                    <el-input v-model="dialogAddSmpForm.remarks" type="textarea" placeholder=""></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" style="text-align:center">
                <el-button size="small" @click="dialogAddSampleVisible = false">关 闭</el-button>
                <el-button size="small" type="primary" @click="handleAddSmpLine('dialogAddSmpForm')">确 认</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import moment from 'moment'
    import {listOption} from "@/const-personal/sample/sampleList.js"
    import {getWarehouseTypes} from '@/api/sysSetup/warehouse'
    import {validateAcquaintance, checkPhone, validatenull} from "@/util/validate.js"
    import {
        DepartmentTreeV,
        getDepartmentTreeOfId,
        DepartmentTreeVChildrenNull,
        getDepartmentTreeOfValue
    } from "@/util/util.js"

    import {
        getSampleTypeInsertList,
        getSampleList,
        getSampleTypeList,
        getSampleTypeListAll,
        addSampleType,
        updateSampleType,
        delSampleTypeById,
        getConditionList,
        addSampleRetentionSave,
        updateSampleRetentionSave,
        delSampleRetentionSaveById,
        insertSample,
        updateSample,
        delSample,
        getSampleRetentionSaveList,
        getSampleTypeListById,
        getSampleRetentionInsert,
        getAllSampleTypeList
    } from "@/api/sample/sampleList.js"

    export default {
        name: "SampleList",
        data() {
            return {
                formSearch: {
                    status: '-1',
                    type: ['-1'],
                    date: [],
                    name: '',
                    entrustUnit: '',
                    client: ''
                },
                tableOption: listOption,
                page: {
                    total: 10, //总页数
                    currentPage: 1, //当前页数
                    pageSize: 10, //每页显示多少条
                    pageSizes: [5, 10]
                },
                tableLoading: false,
                gridData: [], //数据

                // 新增修改
                dialogTitle: '添加样品',
                showDialogAdd: true,  // 显示添加标题或者修改标题dialog
                dialogFormVisible: false,
                rules: {
                    // type: [{message: '请选择样品分类',trigger: 'change'}],
                    name: [{required: true, message: '请输入样品名字', trigger: 'blur'}],
                    specs: [{required: true, message: '请输入样品规格', trigger: 'blur'}],
                    num: [{required: true, validator: validateAcquaintance, trigger: 'blur'}],
                    contactName: [{required: true, message: '请输入委托人名字', trigger: 'blur'}],
                    companyName: [{required: true, message: '请输入委托单位名字', trigger: 'blur'}],
                    phone: [{required: true, validator: checkPhone, trigger: 'blur'}],
                    companyPhone: [{required: true, message: '请输入单位电话', trigger: 'blur'}],
                    date: [{required: true, message: '请输入委托时间', trigger: 'change'}],
                    companyAddress: [{required: true, message: '请输入单位地址', trigger: 'blur'}],
                },
                dialogForm: {
                    type: '',
                    name: '',
                    specs: '',
                    num: '',
                    contactName: '',
                    phone: '',
                    companyName: '',
                    companyPhone: '',
                    date: '',
                    companyAddress: '',
                    remarks: ''
                },
                sampleType: [{id: 1, name: "lei1"}, {id: 2, name: "lei1"}], //样品分类,
                statusList: [
                    {value: '-1', label: '全部'},
                    {value: '0', label: '未留样'},
                    {value: '1', label: '已留样'},
                ], // 留样状态

                // 分类管理
                dialogTypeManageVisible: false,
                treeOption: {
                    children: 'children',
                    label: 'name'
                },
                treeData: [],
                treeDataD: [],
                selectedSample: [], //选中分类

                //增加分类
                dialogFormTypeVisible: false,
                dialogFormType: {
                    parentId: 0,
                    parentIdname: '',
                    content: ''
                },
                rulesType: {
                    parentId: [{required: true, message: '请选择项目分类', trigger: 'change'}],
                    content: [{required: true, message: '请输入分类', trigger: 'blur'}],
                },
                // 修改分类标题
                dialogTypeTitleVisible: false,
                dialogTypeTitle: {
                    name: '',
                    parentId: 0
                },
                rulesTypeTitle: {
                    name: [{required: true, message: "不能为空", trigger: 'blur'}],
                    parentId: [{required: true, message: "不能为空", trigger: 'change'}]
                },
                // 留样条件
                caseTitleAdd: false, //新增或修改留样条件dialog显示控制
                caseData: [{id: 1, name: "lei1"}, {id: 2, name: "lei1"}],
                dialogSampleCaseVisible: false,
                dialogCaseTitleVisible: false,
                dialogCaseTitle: {name: ''},
                rulesCaseTitle: {name: [{required: true, message: "不能为空", trigger: 'blur'}]},
                // 样品留样
                dialogAddSampleVisible: false,
                rulesAddAmp: {
                    store: [{required: true, message: '请选择储存仓库', trigger: 'change'}],
                    condition: [{required: true, message: '请选择储存条件', trigger: 'change'}],
                    name: [{required: true, message: '请输入样品名字', trigger: 'blur'}],
                    num: [{required: true, validator: validateAcquaintance, trigger: 'blur'}],
                    warnTime: [{required: true, validator: validateAcquaintance, trigger: 'blur'}],
                    date: [{required: true, message: '请输入委托时间', trigger: 'change', type: 'date'}],
                    overdueDate: [{required: true, message: '请输入委托时间', trigger: 'change', type: 'date'}],
                },
                dialogAddSmpForm: {},
                warehouseList: [], //储存仓库
                conditionList: [], //储存条件
                sampleTypeList: [], // 样品分类
                sampleTypeTreeSelect: [], // 样品分类
                sampleTypeAllTree: [], // 样品分类

                treeDataDT: [],
                DepartmentTreeVChildrenNullData: []
            }
        },
        methods: {
            // 搜索
            searchFormSubmit() {
                let that = this;
                let sampType = that.formSearch.type
                const sampleTypeId = sampType.length > 1 ? sampType[sampType.length - 1] : sampType[0]
                let data = {
                    page: that.page.currentPage,
                    size: that.page.pageSize,
                    sample: {
                        isRetention: that.formSearch.status === '-1' ? '' : Number(that.formSearch.status),
                        sampleTypeId: sampleTypeId === '-1' ? '' : sampleTypeId,
                        startTime: that.formSearch.date.length ? moment(that.formSearch.date[0]).format('YYYY-MM-DD') : '',
                        endTime: that.formSearch.date.length ? moment(that.formSearch.date[1]).format('YYYY-MM-DD') : '',
                        name: that.formSearch.name || '',
                        entrustUnit: that.formSearch.entrustUnit || '',
                        client: that.formSearch.client || ''
                    }
                }
                getSampleList(data).then(res => {
                    if (res.data.code === 200) {
                        that.gridData = res.data.data.list
                        that.gridData.forEach(item => {
                            const sampleInfo = getDepartmentTreeOfId(that.sampleTypeTreeSelect, item.sampleTypeId)
                            item.sampleType = sampleInfo !== null ? sampleInfo.label : '无'
                        })

                        that.page.total = res.data.data.total
                        that.page.currentPage = res.data.data.pageNum
                        that.page.pageSize = res.data.data.pageSize
                        // that._getList()
                    } else {
                        that.$notify.error({title: '查询失败', message: '获取列表失败！', type: 'danger',});
                    }
                })
            },
            // 重置
            handleReset() {
                this.formSearch= {
                    status: '-1',
                    type: ['-1'],
                    date: [],
                    name: '',
                    entrustUnit: '',
                    client: ''
                }
            },
            // 切换页面
            currentPage(val) {
                console.log(val)
                this.page.currentPage = val
                this._getList()
            },
            sizePage(val) {
                this.page.pageSize = val
                this._getList()
            },
            // 添加或修改dialog显示
            handleAddOrUpdate(row) {
                this.dialogFormVisible = true
                if (row.id) {
                    // 去修改
                    this.showDialogAdd = false
                    const sampleInfo = getDepartmentTreeOfValue(this.sampleTypeTreeSelect, row.sampleTypeId)

                    this.dialogForm = {
                        id: row.id,
                        companyAddress: row.address,
                        contactName: row.client,
                        phone: row.clientPhone,
                        date: moment(row.entrustTime).format('YYYY-MM-DD'),
                        companyName: row.entrustUnit,
                        companyPhone: row.entrustUnitPhone,
                        name: row.name,
                        num: row.numbers,
                        remarks: row.remarks,
                        specs: row.specs,
                        type: sampleInfo
                    }
                    console.log(this.dialogForm)
                } else {
                    // 去添加
                    this.showDialogAdd = true
                    this.dialogForm = {}
                }
            },
            // 确认新增/修改
            handleAddLine(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        const {companyAddress, contactName, phone, date, companyName, companyPhone, name, num, remarks, specs} = this.dialogForm
                        let data = {
                            address: companyAddress,
                            client: contactName,
                            clientPhone: phone,
                            entrustTime: moment(date).format('YYYY-MM-DD'),
                            entrustUnit: companyName,
                            entrustUnitPhone: companyPhone,
                            name: name,
                            numbers: num,
                            remarks: remarks,
                            sampleTypeId: this.selectedSample.length > 1
                                ? this.selectedSample[this.selectedSample.length - 1]
                                : this.selectedSample[0],
                            specs: specs,
                        }
                        if (this.showDialogAdd) {
                            // 添加样品
                            console.log('添加')
                            insertSample(data).then(res => {
                                if (res.data.code === 200) {
                                    this.$notify({title: '操作成功', type: 'success', message: '添加成功'})
                                    this.dialogFormVisible = false
                                    this._getAllTableData()
                                } else {
                                    this.$notify.error({title: '操作失败', type: 'dengar', message: res.data.message})
                                }
                            })
                        } else {
                            // 修改
                            console.log('修改')
                            data = {...data, id: this.dialogForm.id}

                            updateSample(data).then(res => {
                                if (res.data.code === 200) {
                                    this.$notify({title: '操作成功', type: 'success', message: '添加成功'})
                                    this.dialogFormVisible = false
                                    this._getAllTableData()
                                } else {
                                    this.$notify.error({title: '操作失败', type: 'dengar', message: res.data.message})
                                }
                            })
                        }
                    }
                })
            },
            // 修改
            handleEdit(row, index) {
                this.dialogFormVisible = true;
                this.dialogTitle = '修改样品';
            },
            // 删除
            handleDelete(row, index) {
                this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
                }).then(() => {
                    //请求--删除
                    const data = {id: row.id}
                    console.log(row.id)
                    delSample(data).then(res => {
                        if (res.data.code === 200) {
                            this.getTableData()
                            this.$notify.success('删除成功')
                        } else {
                            this.$notify.error('删除失败')
                        }
                    })
                })
            },
            // 打印
            handlePrint(row, index) {
            },
            // 留样
            handleKeep(row, index) {
                this.dialogAddSampleVisible = true
                this.dialogAddSmpForm = {
                    id: row.id
                }
            },
            // 点击分类管理
            typeManage() {
                this.dialogTypeManageVisible = true
                // 获取样品分类列表
                this._getSampleTypeList();
            },
            // 新增树--类型
            appendTreeType() {
                this.dialogFormTypeVisible = true;
            },
            handleNodeClick(data) {
            },
            handleNodeClick2(data) {
                console.log(data)
                this.dialogTypeTitle.parentId = data.id;
                this.dialogTypeTitle.parentIdname = data.name;
            },
            // 选中分类
            handleSelected(value) {
                console.log(value)
                this.selectedSample = value
            },
            // 添加分类标题
            addTree(node, data) {
                this.dialogFormType.parentIdname = data.name
                this.dialogFormType.parentId = validatenull(data.id) ? null : data.id;
                this.dialogFormType.name = validatenull(data.name) ? null : data.name;
                this.dialogFormTypeVisible = true;
            },
            addTreeNoP(node, data) {
                this.dialogFormType.parentId = null;
                this.dialogFormType.parentIdname = null;
                this.dialogFormTypeVisible = true;
            },
            // 修改分类标题
            editTree(node, data) {
                let parent = getDepartmentTreeOfId(this.sampleTypeList, data.parentId)
                this.dialogTypeTitle.parentId = data.parentId;
                this.dialogTypeTitle.parentIdname = validatenull(parent) ? null : parent.name;
                this.dialogTypeTitle.name = data.name;
                this.dialogTypeTitle.id = data.id;
                this.dialogTypeTitleVisible = true;
            },
            // 删除分类
            removeTree(node, data) {
                this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(() => {
                    delSampleTypeById({id: data.id}).then(res => {
                        if (res.data.code === 200) {
                            this.$notify({title: '操作成功', type: 'success', message: '已成功删除!'});
                            this._getSampleTypeList()
                        } else {
                            this.$notify.error({title: '操作失败', message: res.data.message, type: 'danger',});
                        }
                    })
                }).catch(() => {
                    console.log("已取消删除")
                })
            },
            // 确认修改树
            handleEditTypeTitle(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        //xxx
                        updateSampleType({
                            parentId: this.dialogTypeTitle.parentId,
                            name: this.dialogTypeTitle.name,
                            id: this.dialogTypeTitle.id,
                        }).then(res => {
                            if (res.data.code == 200) {
                                this.$refs[formName].resetFields();
                                this.$notify({title: '操作成功', type: 'success', message: '已修改成功!'});
                                this.dialogTypeTitleVisible = false;
                                this._getSampleTypeList()
                            } else {
                                this.$notify.error({title: '操作失败', type: 'danger', message: res.data.message});
                            }
                        })
                    }
                })
            },
            // 增加分类--选中树
            handleTree(value) {
                console.log(this.dialogTypeTitle.parentId)
                //  this.dialogFormType.parentId=value;
            },
            // 增加分类---input 回车事件
            changeInput(value) {
            },
            // 确认新增-分类--树
            handleAddType(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        // let text = this.dialogFormType.content.replace(/\n/g, ';')
                        // let textArr = text.split(';')
                        // textArr = textArr.filter(item => item !== '')

                        // 0101 问题所在
                        let data = {}
                        this.dialogFormType.parentId !== null
                            ? data = {name: this.dialogFormType.content, parentId: this.dialogFormType.parentId}
                            : data = {name: this.dialogFormType.content}

                        addSampleType(data).then(res => {
                            if (res.data.code == 200) {
                                this.$refs[formName].resetFields();
                                this.$notify({title: '操作成功', type: 'success', message: '已成功添加!'});
                                this.dialogFormTypeVisible = false;
                                this._getSampleTypeList()
                            } else {
                                this.$notify.error({title: '操作失败', type: 'danger', message: res.data.message});
                            }
                        })
                    }
                })
            },
            // 点击留样条件
            SampleCaseManage() {
                this.dialogSampleCaseVisible = true
            },
            // 新增留样条件
            appendSampleCase(row) {
                this.dialogCaseTitleVisible = true
                this.dialogCaseTitle = {
                    id: '',
                    name: ''
                }
            },
            // 修改留样条件
            editSampleCase(row, index) {
                this.dialogCaseTitleVisible = true;
                this.caseTitleAdd = true
                this.dialogCaseTitle = {
                    id: row.value,
                    name: row.label
                }
            },
            // 删除留样条件
            deleteSampleCase(row, index) {
                delSampleRetentionSaveById({id: row.value}).then(res => {
                    if (res.data.code === 200) {
                        this._getConditionList()
                        this.$notify({title: '操作成功', type: 'success', message: '删除成功'})
                    } else {
                        this.$notify.error({title: '操作失败', type: 'danger', message: res.data.message})
                    }
                })
            },
            // 确认新增/修改--留样条件
            handleCaseTitle(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        // 新增
                        const data = {
                            id: this.dialogCaseTitle.id,
                            name: this.dialogCaseTitle.name
                        }
                        if (!this.dialogCaseTitle.id) {
                            addSampleRetentionSave(data).then(res => {
                                if (res.data.code === 200) {
                                    this.dialogCaseTitleVisible = false
                                    // 刷新
                                    this._getConditionList()
                                    this.$refs[formName].resetFields()
                                }
                            })
                        } else {
                            // 修改
                            updateSampleRetentionSave(data).then(res => {
                                if (res.data.code === 200) {
                                    this.dialogCaseTitleVisible = false
                                    // 刷新
                                    this._getConditionList();
                                    this.$refs[formName].resetFields()
                                }
                            })
                        }
                    }
                })
            },
            // 确认留样
            handleAddSmpLine(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        console.log(this.dialogAddSmpForm)
                        const data = {
                            sampleId: this.dialogAddSmpForm.id,
                            numbers: Number(this.dialogAddSmpForm.num),
                            people: this.dialogAddSmpForm.name,
                            startTime: moment(this.dialogAddSmpForm.date).format('YYYY-MM-DD'),
                            endTime: moment(this.dialogAddSmpForm.overdueDate).format('YYYY-MM-DD'),
                            warning: Number(this.dialogAddSmpForm.warnTime),
                            warehouseId: this.dialogAddSmpForm.store,
                            sampleRetentionSaveId: this.dialogAddSmpForm.condition,
                            remarks: this.dialogAddSmpForm.remarks
                        }
                        getSampleRetentionInsert(data).then(res => {
                            if (res.data.code === 200) {
                                this.dialogAddSampleVisible = false
                                this.$refs[formName].resetFields()
                                this._getAllTableData()
                                this.$notify({title: '查询失败', message:"留样成功!", type: 'success',})
                            } else {
                                this.$notify.error({title: '查询失败', message: res.data.message, type: 'danger',})
                            }
                        })
                    }
                })
            },
            // 获取储存条件
            _getConditionList() {
                const data = {
                    page: this.page.currentPage,
                    size: this.page.pageSize
                }

                getSampleRetentionSaveList(data).then(res => {
                    if (res.data.code === 200) {
                        const result = res.data.data.list
                        let info = []
                        if (result.length) {
                            result.forEach(item => {
                                info.push({value: item.id, label: item.name})
                            })
                            this.conditionList = info
                        }
                    } else {
                        this.$notify.error(res.data.message)
                    }
                })
            },
            // 获取储存仓库
            _getWarehouseList(data) {
                getWarehouseTypes(data).then(res => {
                    if (res.data.code === 200) {
                        let result = res.data.data
                        let info = []
                        if (result.length) {
                            result.map(item => {
                                info.push({value: item.id, label: item.name})
                            })
                        }
                        this.warehouseList = info
                    } else {
                        this.$notify.error(res.data.message)
                    }
                })
            },
            // 获取样品分类
            _getSampleTypeList() {
                getAllSampleTypeList().then(res => {
                    if (res.data.code == 200) {
                        this.sampleTypeList = res.data.data
                        this.sampleTypeTreeSelect = DepartmentTreeVChildrenNull(this.sampleTypeList)
                        this.sampleTypeAllTree = DepartmentTreeVChildrenNull(this.sampleTypeList)
                    }
                })
            },
            // 获取列表数据
            _getList() {
                let data = {
                    page: this.page.currentPage,
                    size: this.page.pageSize,
                    sample: {}
                }
                getSampleList(data).then(res => {
                    if (res.data.code === 200) {
                        this.gridData = res.data.data.list

                        this.gridData.forEach(item => {
                            const sampleInfo = getDepartmentTreeOfId(this.sampleTypeTreeSelect, item.sampleTypeId)
                            item.sampleType = sampleInfo !== null ? sampleInfo.label : '无'
                        })

                        this.page.total = res.data.data.total
                        this.page.currentPage = res.data.data.pageNum
                        this.page.pageSize = res.data.data.pageSize
                    } else {
                        this.$notify.error({title: '查询失败', message: '获取列表失败！', type: 'danger',})
                    }
                })
            },
            // 样品分类、储存仓库、储存条件、表格数据、留样条件
            _getAllTableData() {
                const data = {
                    page: this.page.currentPage,
                    size: this.page.pageSize
                }

                getAllSampleTypeList().then(res => {
                    if (res.data.code === 200) {
                        this.sampleTypeList = res.data.data
                        this.sampleTypeTreeSelect = DepartmentTreeVChildrenNull(res.data.data, [{
                            label: '全部',
                            value: '-1'
                        }])
                        this.sampleTypeAllTree = DepartmentTreeVChildrenNull(res.data.data)

                        getWarehouseTypes(data).then(res => {
                            if (res.data.code === 200) {
                                let result = res.data.data
                                let info = []
                                if (result.length) {
                                    result.map(item => {
                                        info.push({value: item.id, label: item.name})
                                    })
                                    this.warehouseList = info
                                    this._getList() // 样品
                                }
                            } else {
                                this.$notify.error(res.data.message)
                            }
                        })
                    }
                })

                // this._getSampleTypeList()  // 样品分类
                // this._getWarehouseList(data)  // 储存仓库
                this._getConditionList()  // 留样储存条件

            },
        },
        mounted() {
            this._getAllTableData()
        }
    }
</script>

<style scoped>
    .add-type {
        padding-bottom: 20px;
        border-bottom: 1px #333 dotted;
        margin-bottom: 20px;
        margin-top: -20px
    }

    .custom-tree-node {
        display: flex;
        justify-content: space-between;
        flex-direction: row;
        width: 100%;
        align-items: center;
    }
</style>
